<?php

namespace app\logic\admin;

class L_html
{
    public $cache_dir="cache/";

    /**
     * 模块首页生成缓存
     * @param string $paramObj
     */
    public function module_index($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'moduleId' => 'require',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            //获取模块数据
            $moduleId=$dataObj->request->moduleId;
            $moduleData=$moduleId?module($moduleId):array();
            $index_html=setting("moduleIndexHtml",$moduleId);
            $index=siteSetting("index_filename");
            if($index_html){
                initSeo("index",$moduleId);

                //获取模板配置
                $template_index=setting("template_index",$moduleId);
                $template=$template_index?$template_index:"index/article/index";

                //模板路径
                $template_path=setting("template_path",$moduleId);
                $template_path=$template_path?$template_path:"";

                $data=array();
                $data["module"]=$moduleData;
                $data["request"]=array(
                    "moduleId"=>$dataObj->request->moduleId,
                );
                $fileCon=ob_template($template,$data,$template_path);
                //获取模型url规则
                $public=publicPath();
                $filename=$public."{$this->cache_dir}{$moduleData["urlPath"]}/{$index}.html";
                fileWrite($filename,$fileCon);
            }else{
                //删除缓存文件
                $public=publicPath();
                $filename=$public."{$this->cache_dir}{$moduleData["urlPath"]}/{$index}.html";
                if(is_file($filename)){
                    unlink($filename);
                }
            }
        }
        return $final_arr;
    }

    /**
     * 模块列表页生成缓存
     * @param string $paramObj
     */
    public function module_list($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'moduleId' => 'require',
                'page' => 'require',
                'pagesize' => 'require',
                'nums' => 'require',
                'categoryId' => 'require',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $dataObj->data->pass=false;
            $index_html=setting("moduleListHtml",$dataObj->request->moduleId);
            if($index_html){
                $dataObj->data->pass=true;
            }
        }
        if ($final_arr["error_no"] == 0) {
            if($dataObj->data->pass){
                if ($final_arr["error_no"] == 0) {
                    $paramsObj=paramsObj();
                    $paramsObj->params=array(
                        'id' => $dataObj->request->categoryId,
                        'verify' => 1,
                    );
                    $checkResult=app("S_Admin_category")->info($paramsObj);
                    if ($checkResult["error_no"] == 0) {
                        $dataObj->data->categoryInfo=$checkResult["result"];
                    }else{
                        $final_arr = logCallErrorMsg($final_arr, $checkResult);
                    }
                }
                if ($final_arr["error_no"] == 0) {
                    if($dataObj->request->page == 1) {
                        //检测分类数据中的 item是否正确，不正确则更新。
                        $paramsObj=paramsObj();
                        $paramsObj->params=array(
                            "moduleId"=>$dataObj->request->moduleId,
                            "categoryId"=>$dataObj->data->categoryInfo["id"],
                        );
                        $checkResult=app("S_Admin_article")->getCountByCategory($paramsObj);
                        if ($checkResult["error_no"] == 0) {
                            $dataObj->data->item=$checkResult["result"];
                            if($dataObj->data->item != $dataObj->data->categoryInfo["item"]){
                                //更新分类字段数量
                                $paramsObj=paramsObj();
                                $paramsObj->params=array(
                                    'id' => $dataObj->data->categoryInfo["id"],
                                    'item' => $dataObj->data->item,
                                );
                                $checkResult=app("S_Admin_category")->updateItemField($paramsObj);
                                if ($checkResult["error_no"] == 0) {

                                }else{
                                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                                }
                            }
                        }else {
                            $final_arr = logCallErrorMsg($final_arr, $checkResult);
                        }
                    }else{
                        $dataObj->data->item=$dataObj->data->categoryInfo["item"];
                    }
                }
                if ($final_arr["error_no"] == 0) {
                    $total = max(ceil($dataObj->data->item/$dataObj->request->pagesize), 1);
                    $page=$dataObj->request->page;
                    $nums=$dataObj->request->nums;
                    $currentTotal=$page+$nums-1;
                    $total=$total < $currentTotal?$total:$currentTotal;

                    $org_route=request()->route(false);
                    for(; $page <= $total; $page++) {
                        if ($final_arr["error_no"] == 0) {
                            //设置路由
                            request()->setRoute(array("moduleId"=>$dataObj->request->moduleId,"categoryId"=>$dataObj->data->categoryInfo["id"],"page"=>$page,"size"=>$dataObj->request->pagesize));
                            $this->catchListTemplate($dataObj->request->moduleId,$dataObj->data->categoryInfo["id"],$page);
                        }
                        if ($final_arr["error_no"]) {
                            break;
                        }
                    }
                    request()->withRoute(array());
                    request()->setRoute($org_route);
                }
            }
        }
        return $final_arr;
    }

    /**
     * 模块内容页生成缓存
     * @param string $paramObj
     */
    public function module_show($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'articleData' => 'require',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            //设置路由
            $org_route=request()->route(false);
            request()->setRoute(array("moduleId"=>$dataObj->request->articleData["moduleId"],"articleId"=>$dataObj->request->articleData["id"]));
            $this->catchDetailTemplate();
            request()->withRoute(array());
            request()->setRoute($org_route);
        }
        return $final_arr;
    }

    /**
     * 缓存列表模板
     * @param $moduleId
     * @param $categoryId
     * @param $page
     */
    public function catchListTemplate($moduleId,$categoryId,$page){
        $moduleData=$moduleId?module($moduleId):array();

        $categoryData=($moduleId && $categoryId)?category('module',$moduleId,$categoryId):array();
        if($categoryData){
            $categoryData["linkUrl"]=$moduleData['linkUrl'].$categoryData['linkurl'];
        }

        initSeo("list",$moduleId,$categoryId,$page);

        //获取模板配置
        $template_list=setting("template_list",$moduleId);
        $template="index/article/list";
        if(isset($categoryData["template_list"]) && $categoryData["template_list"]){
            $template=$categoryData["template_list"];
        }else if($template_list){
            $template=$template_list;
        }

        //模板路径
        $template_path=setting("template_path",$moduleId);
        $template_path=$template_path?$template_path:"";

        $data=array();
        $data["module"]=$moduleData;
        $data["category"]=$categoryData;
        $data["request"]=array(
            "catId"=>$categoryId,
            "moduleId"=>$moduleId,
        );
        $fileCon=ob_template($template,$data,$template_path);
        //获取模型url规则
        $public=publicPath();
        $filename=$public."{$this->cache_dir}{$moduleData["urlPath"]}/".listurl($moduleId,$categoryId,$page);
        fileWrite($filename,$fileCon);
    }

    /**
     * 缓存内容模板
     * @param $moduleId
     * @param $categoryId
     * @param $page
     */
    public function catchDetailTemplate(){
        $articleId=request()->param("articleId");
        $paramsObj = paramsObj();
        $paramsObj->params = array(
            "id"=>$articleId,
        );
        $response = app("C_Index_article")->info($paramsObj);
        $articleData = array();
        if ($response["success"]) {
            $articleData = $response["data"];
        }

        $moduleId=request()->param("moduleId");
        $moduleData=$moduleId?module($moduleId):array();
        $categoryData=($moduleId && isset($articleData["categoryId"]) && $articleData["categoryId"])?category('module',$moduleId,$articleData["categoryId"]):array();
        $categoryData["linkUrl"]=$moduleData['linkUrl'].$categoryData['linkurl'];

        initSeo("detail",$moduleId,0,0,$articleData);

        //获取模板配置
        $template_content=setting("template_content",$moduleId);
        $template="index/article/detail";
        if(isset($categoryData["template_content"]) && $categoryData["template_content"]){
            $template=$categoryData["template_content"];
        }else if($template_content){
            $template=$template_content;
        }

        $data=array();
        $data["module"]=$moduleData;
        $data["article"]=$articleData;
        $data["category"]=$categoryData;
        $data["request"]=request()->param();

        //模板路径
        $template_path=setting("template_path",$moduleId);
        $template_path=$template_path?$template_path:"";

        $fileCon=ob_template($template,$data,$template_path);

        //获取模型url规则
        $public=publicPath();
        $filename=$public."{$this->cache_dir}{$moduleData["urlPath"]}/".itemurl($articleData);
        fileWrite($filename,$fileCon);
    }
}